package com.genexususa.eo.bing.connection;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.*;
import com.genexususa.eo.bing.data.GxException;
import com.genexususa.eo.bing.data.Info;
import com.genexususa.eo.bing.data.Util;
import com.genexususa.eo.bing.data.results.GxIdResult;
import com.genexususa.eo.bing.data.results.GxIdResults;
import com.genexususa.eo.bing.data.wsSubclasses.GxAdGroup;
import com.genexususa.eo.bing.data.wsSubclasses.GxCampaign;
import com.genexususa.eo.bing.data.wsSubclasses.GxCampaignNegativeKeywords;
import com.genexususa.eo.bing.data.wsSubclasses.GxCampaignNegativeSites;
import com.genexususa.eo.bing.test.TestUtil;
import com.genexususa.eo.google.data.*;
import com.genexususa.eo.google.data.Error;
import org.xml.sax.SAXException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.rpc.ServiceException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Properties;

public class CampaignManager {
    public CampaignManager() {
    }

    public String addCampaigns(long customerAccountId, String campaignsXML) {
        ICampaignManagementService campaignManagement = null;
        CampaignManagementServiceLocator campaignServiceLocator = null;
        BasicHttpBinding_ICampaignManagementServiceStub stub = null;
        AddCampaignsRequest addCampaignsRequest = null;
        AddCampaignsResponse addCampaignsResponse = null;
//        GxIdResults result = null;
        GA_Result result = new GA_Result();
        Util util = new Util();
        Campaign[] campaigns = null;
        try {
            HashMap<String, String> properties = Info.getBingProperties();
            String namespace = properties.get("campaignNamespace");
            String url = properties.get("campaignURL");
            String userName = properties.get("username");
            String password = properties.get("password");
            String developerToken = properties.get("developerToken");
            String customerId = properties.get("customerId");
            String applicationToken = properties.get("applicationToken");

            campaignServiceLocator = new CampaignManagementServiceLocator();
            campaignServiceLocator.setBasicHttpBinding_ICampaignManagementServiceEndpointAddress(url);
            campaignManagement = campaignServiceLocator.getBasicHttpBinding_ICampaignManagementService(); // ServiceException
            stub = (BasicHttpBinding_ICampaignManagementServiceStub) campaignManagement;

            // Set header parameters
            stub.setHeader(namespace, "UserName", userName);
            stub.setHeader(namespace, "Password", password);
            stub.setHeader(namespace, "DeveloperToken", developerToken);
            stub.setHeader(namespace, "CustomerId", customerId);
            stub.setHeader(namespace, "CustomerAccountId", customerAccountId);
            stub.setHeader(namespace, "ApplicationToken", applicationToken);

            // Set body parameters
            addCampaignsRequest = new AddCampaignsRequest();
            addCampaignsRequest.setAccountId(customerAccountId);
            // Parse campaigns from XML
            campaigns = util.campaignsFromXML(campaignsXML); // IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException
            //addCampaignsRequest.setCampaigns(campaigns);

            Campaign[] allCampaigns = campaigns;
            Campaign[] chunkedCampaigns = null;
            // max element limit = 1000
            int chunks = (int)Math.ceil(allCampaigns.length / 1000.0);
            if (chunks <= 1){
                addCampaignsRequest.setCampaigns(allCampaigns);

                addCampaignsResponse = campaignManagement.addCampaigns(addCampaignsRequest); // RemoteException, EditorialApiFaultDetail, AdApiFaultDetail

                long[] campaignIds = addCampaignsResponse.getCampaignIds();

                for (int i=0; i<campaignIds.length; i++) {
                    GA_ResultItem resultItem = new GA_ResultItem();
                    resultItem.SubCampaignGxId =  ((GxCampaign)addCampaignsRequest.getCampaigns()[i]).getGxId();
                    resultItem.CampaignId =  campaignIds[i];

                    result.Items.add(resultItem);
                }
            }else if(chunks > 1){
                int counter = 1;
                int index = 0;
                int chunkItems = 1000;
                while (counter <= chunks){
                    if(counter == chunks){
                        chunkItems = allCampaigns.length - index;
                    }

                    chunkedCampaigns = new Campaign[chunkItems];
                    for(int i = 0; i < chunkItems ; i++){
                        chunkedCampaigns[i] = allCampaigns[index];
                        index+=1;
                    }
                    index = counter * 1000;
                    counter++;

                    addCampaignsRequest.setCampaigns(chunkedCampaigns);

                    addCampaignsResponse = campaignManagement.addCampaigns(addCampaignsRequest); // RemoteException, EditorialApiFaultDetail, AdApiFaultDetail

                    long[] campaignIds = addCampaignsResponse.getCampaignIds();
                    //result = new GxIdResults(trackingId, campaignIds.length);
                    for (int i=0; i<addCampaignsRequest.getCampaigns().length; i++) {
                        GA_ResultItem resultItem = new GA_ResultItem();
                        resultItem.SubCampaignGxId =  ((GxCampaign)addCampaignsRequest.getCampaigns()[i]).getGxId();
                        resultItem.CampaignId =  (addCampaignsRequest.getCampaigns()[i]).getId();

                        result.Items.add(resultItem);
                    }
                }
            }


            // Call WS
            //addCampaignsResponse = campaignManagement.addCampaigns(addCampaignsRequest); // RemoteException, ApiFaultDetail, AdApiFaultDetail

            // Call succeeded with no errors

            // Create the return object
            /*trackingId = stub.getResponseHeader(namespace, "TrackingId").getValue();
            long[] campaignIds = addCampaignsResponse.getCampaignIds();
            result = new GxIdResults(trackingId, campaignIds.length);
            for (int i=0; i<campaignIds.length; i++) {
                result.addResult(i, new GxIdResult(((GxCampaign)campaigns[i]).getGxId(), campaignIds[i]));
            }
            */
        } catch (GxException e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        } catch (ApiFaultDetail fault) { // IN CALL
            for (int i=0; i<addCampaignsRequest.getCampaigns().length; i++) {
                GA_ResultItem resultItem = new GA_ResultItem(fault, i);

                resultItem.SubCampaignGxId = ((GxCampaign)addCampaignsRequest.getCampaigns()[i]).getGxId();

                result.Items.add(resultItem);
            }
        } catch (AdApiFaultDetail fault) { // IN CALL
            for (int i=0; i<addCampaignsRequest.getCampaigns().length; i++) {
                GA_ResultItem resultItem = new GA_ResultItem(fault);

                resultItem.SubCampaignGxId = ((GxCampaign)addCampaignsRequest.getCampaigns()[i]).getGxId();

                result.Items.add(resultItem);
            }
        } catch (RemoteException e) { // IN CALL
            for (int i=0; i<addCampaignsRequest.getCampaigns().length; i++) {
                GA_ResultItem resultItem = new GA_ResultItem(e);

                resultItem.SubCampaignGxId = ((GxCampaign)addCampaignsRequest.getCampaigns()[i]).getGxId();

                result.Items.add(resultItem);
            }
        } catch (Exception e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        }

        String returnMessage = null;
        // GxIdResults -> XML
        try {
            JAXBContext contextObj = JAXBContext.newInstance(GA_Result.class); // JAXBException

            Marshaller marshallerObj = contextObj.createMarshaller(); // JAXBException
            marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // PropertyException

            StringWriter sw = new StringWriter();

            marshallerObj.marshal(result, sw); // JAXBException
            returnMessage = sw.getBuffer().toString();
        }  catch (PropertyException e) { // AFTER CALL
            returnMessage = util.getPropertyExceptionMessage(e);
        } catch (JAXBException e) { // AFTER CALL
            returnMessage = util.getJAXBExceptionMessage(e);
        }
        return returnMessage;
    }

    public String updateCampaigns(long customerAccountId, String campaignsXML) {
        ICampaignManagementService campaignManagement = null;
        CampaignManagementServiceLocator campaignServiceLocator = null;
        BasicHttpBinding_ICampaignManagementServiceStub stub = null;
        UpdateCampaignsRequest updateCampaignsRequest = null;
        GA_Result result = new GA_Result();
        Util util = new Util();
        Campaign[] campaigns = null;
        try {
            HashMap<String, String> properties = Info.getBingProperties();
            String namespace = properties.get("campaignNamespace");
            String url = properties.get("campaignURL");
            String userName = properties.get("username");
            String password = properties.get("password");
            String developerToken = properties.get("developerToken");
            String customerId = properties.get("customerId");
            String applicationToken = properties.get("applicationToken");

            campaignServiceLocator = new CampaignManagementServiceLocator();
            campaignServiceLocator.setBasicHttpBinding_ICampaignManagementServiceEndpointAddress(url);
            campaignManagement = campaignServiceLocator.getBasicHttpBinding_ICampaignManagementService(); // ServiceException
            stub = (BasicHttpBinding_ICampaignManagementServiceStub) campaignManagement;

            // Set header parameters
            stub.setHeader(namespace, "UserName", userName);
            stub.setHeader(namespace, "Password", password);
            stub.setHeader(namespace, "DeveloperToken", developerToken);
            stub.setHeader(namespace, "CustomerId", customerId);
            stub.setHeader(namespace, "CustomerAccountId", customerAccountId);
            stub.setHeader(namespace, "ApplicationToken", applicationToken);

            // Set body parameters
            updateCampaignsRequest = new UpdateCampaignsRequest();
            updateCampaignsRequest.setAccountId(customerAccountId);
            // Parse campaigns from XML
            campaigns = util.campaignsFromXML(campaignsXML); // IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException
            updateCampaignsRequest.setCampaigns(campaigns);

            // Call WS
            campaignManagement.updateCampaigns(updateCampaignsRequest); // RemoteException, ApiFaultDetail, AdApiFaultDetail

            // Call succeeded with no errors

            // Create the return object

            for (int i=0; i<updateCampaignsRequest.getCampaigns().length; i++){
                GA_ResultItem resultItem = new GA_ResultItem();
                resultItem.SubCampaignGxId = ((GxCampaign)updateCampaignsRequest.getCampaigns()[i]).getGxId();
                resultItem.CampaignId = (updateCampaignsRequest.getCampaigns()[i]).getId();

                result.Items.add(resultItem);
            }
        } catch (GxException e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        } catch (ApiFaultDetail fault) { // IN CALL
            for (int i=0; i<updateCampaignsRequest.getCampaigns().length; i++) {
                GA_ResultItem resultItem = new GA_ResultItem(fault, i);

                resultItem.SubCampaignGxId = ((GxCampaign)updateCampaignsRequest.getCampaigns()[i]).getGxId();

                result.Items.add(resultItem);
            }
        } catch (AdApiFaultDetail fault) { // IN CALL
            for (int i=0; i<updateCampaignsRequest.getCampaigns().length; i++) {
                GA_ResultItem resultItem = new GA_ResultItem(fault);

                resultItem.SubCampaignGxId =  ((GxCampaign)updateCampaignsRequest.getCampaigns()[i]).getGxId();

                result.Items.add(resultItem);
            }
        } catch (RemoteException e) { // IN CALL
            for (int i=0; i<updateCampaignsRequest.getCampaigns().length; i++) {
                GA_ResultItem resultItem = new GA_ResultItem(e);

                resultItem.SubCampaignGxId = ((GxCampaign)updateCampaignsRequest.getCampaigns()[i]).getGxId();

                result.Items.add(resultItem);
            }
        } catch (Exception e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        }

        String returnMessage = null;
        // GxIdResults -> XML
        try {
            JAXBContext contextObj = JAXBContext.newInstance(GA_Result.class); // JAXBException

            Marshaller marshallerObj = contextObj.createMarshaller(); // JAXBException
            marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // PropertyException

            StringWriter sw = new StringWriter();

            marshallerObj.marshal(result, sw); // JAXBException
            returnMessage = sw.getBuffer().toString();
        }  catch (PropertyException e) { // AFTER CALL
            returnMessage = util.getPropertyExceptionMessage(e);
        } catch (JAXBException e) { // AFTER CALL
            returnMessage = util.getJAXBExceptionMessage(e);
        }
        return returnMessage;
    }

    public String setNegativeKeywordsToCampaigns(long customerAccountId, String campaignNegativeKeywordsXML) {
        ICampaignManagementService campaignManagement = null;
        CampaignManagementServiceLocator campaignServiceLocator = null;
        BasicHttpBinding_ICampaignManagementServiceStub stub = null;
        SetNegativeKeywordsToCampaignsRequest setNegativeKeywordsToCampaignsRequest = null;
        GA_Result result = new GA_Result();
        Util util = new Util();
        CampaignNegativeKeywords[] campaignNegativeKeywords = null;
        try {
            HashMap<String, String> properties = Info.getBingProperties();
            String namespace = properties.get("campaignNamespace");
            String url = properties.get("campaignURL");
            String userName = properties.get("username");
            String password = properties.get("password");
            String developerToken = properties.get("developerToken");
            String customerId = properties.get("customerId");
            String applicationToken = properties.get("applicationToken");

            campaignServiceLocator = new CampaignManagementServiceLocator();
            campaignServiceLocator.setBasicHttpBinding_ICampaignManagementServiceEndpointAddress(url);
            campaignManagement = campaignServiceLocator.getBasicHttpBinding_ICampaignManagementService(); // ServiceException
            stub = (BasicHttpBinding_ICampaignManagementServiceStub) campaignManagement;

            // Set header parameters
            stub.setHeader(namespace, "UserName", userName);
            stub.setHeader(namespace, "Password", password);
            stub.setHeader(namespace, "DeveloperToken", developerToken);
            stub.setHeader(namespace, "CustomerId", customerId);
            stub.setHeader(namespace, "CustomerAccountId", customerAccountId);
            stub.setHeader(namespace, "ApplicationToken", applicationToken);

            // Set body parameters
            setNegativeKeywordsToCampaignsRequest = new SetNegativeKeywordsToCampaignsRequest();
            setNegativeKeywordsToCampaignsRequest.setAccountId(customerAccountId);
            // Parse campaignNegativeKeywords from XML
            campaignNegativeKeywords = util.campaignNegativeKeywordsFromXML(campaignNegativeKeywordsXML); // IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException
            setNegativeKeywordsToCampaignsRequest.setCampaignNegativeKeywords(campaignNegativeKeywords);

            // Call WS
            campaignManagement.setNegativeKeywordsToCampaigns(setNegativeKeywordsToCampaignsRequest); // RemoteException, ApiFaultDetail, AdApiFaultDetail

            // Call succeeded with no errors

            // Create the return object

            //result = new GxIdResults(trackingId, campaignNegativeKeywords.length);
            for (int i=0; i<campaignNegativeKeywords.length; i++) {
                GA_ResultItem resultItem =  new GA_ResultItem();
                resultItem.SubCampaignGxId = ((GxCampaignNegativeKeywords) campaignNegativeKeywords[i]).getGxCampaignId();
                resultItem.CampaignId = campaignNegativeKeywords[i].getCampaignId();
                //result.addResult(i, new GxIdResult(((GxCampaignNegativeKeywords) campaignNegativeKeywords[i]).getGxCampaignId(), campaignNegativeKeywords[i].getCampaignId()));
                result.Items.add(resultItem);
            }
        } catch (GxException e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        } catch (ApiFaultDetail fault) { // IN CALL
            for (int i=0; i<setNegativeKeywordsToCampaignsRequest.getCampaignNegativeKeywords().length; i++) {
                GA_ResultItem resultItem = new GA_ResultItem(fault, i);

                resultItem.SubCampaignGxId =  ((GxCampaignNegativeKeywords)setNegativeKeywordsToCampaignsRequest.getCampaignNegativeKeywords()[i]).getGxCampaignId();

                result.Items.add(resultItem);
            }
        } catch (AdApiFaultDetail fault) { // IN CALL
            for (int i=0; i<setNegativeKeywordsToCampaignsRequest.getCampaignNegativeKeywords().length; i++) {
                GA_ResultItem resultItem = new GA_ResultItem(fault);

                resultItem.SubCampaignGxId =  ((GxCampaignNegativeKeywords)setNegativeKeywordsToCampaignsRequest.getCampaignNegativeKeywords()[i]).getGxCampaignId();

                result.Items.add(resultItem);
            }
        } catch (RemoteException e) { // IN CALL
            for (int i=0; i<setNegativeKeywordsToCampaignsRequest.getCampaignNegativeKeywords().length; i++) {
                GA_ResultItem resultItem = new GA_ResultItem(e);

                resultItem.SubCampaignGxId =  ((GxCampaignNegativeKeywords)setNegativeKeywordsToCampaignsRequest.getCampaignNegativeKeywords()[i]).getGxCampaignId();

                result.Items.add(resultItem);
            }
        } catch (Exception e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        }

        String returnMessage = null;
        // GxIdResults -> XML
        try {
            JAXBContext contextObj = JAXBContext.newInstance(GA_Result.class); // JAXBException

            Marshaller marshallerObj = contextObj.createMarshaller(); // JAXBException
            marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // PropertyException

            StringWriter sw = new StringWriter();

            marshallerObj.marshal(result, sw); // JAXBException
            returnMessage = sw.getBuffer().toString();
        }  catch (PropertyException e) { // AFTER CALL
            returnMessage = util.getPropertyExceptionMessage(e);
        } catch (JAXBException e) { // AFTER CALL
            returnMessage = util.getJAXBExceptionMessage(e);
        }
        return returnMessage;
    }

    public String setNegativeSitesToCampaigns(long customerAccountId, String campaignNegativeSitesXML) {
        ICampaignManagementService campaignManagement = null;
        CampaignManagementServiceLocator campaignServiceLocator = null;
        BasicHttpBinding_ICampaignManagementServiceStub stub = null;
        SetNegativeSitesToCampaignsRequest setNegativeSitesToCampaignsRequest = null;
        GA_Result result = new GA_Result();
        Util util = new Util();
        CampaignNegativeSites[] campaignNegativeSites = null;
        try {
            HashMap<String, String> properties = Info.getBingProperties();
            String namespace = properties.get("campaignNamespace");
            String url = properties.get("campaignURL");
            String userName = properties.get("username");
            String password = properties.get("password");
            String developerToken = properties.get("developerToken");
            String customerId = properties.get("customerId");
            String applicationToken = properties.get("applicationToken");

            campaignServiceLocator = new CampaignManagementServiceLocator();
            campaignServiceLocator.setBasicHttpBinding_ICampaignManagementServiceEndpointAddress(url);
            campaignManagement = campaignServiceLocator.getBasicHttpBinding_ICampaignManagementService(); // ServiceException
            stub = (BasicHttpBinding_ICampaignManagementServiceStub) campaignManagement;

            // Set header parameters
            stub.setHeader(namespace, "UserName", userName);
            stub.setHeader(namespace, "Password", password);
            stub.setHeader(namespace, "DeveloperToken", developerToken);
            stub.setHeader(namespace, "CustomerId", customerId);
            stub.setHeader(namespace, "CustomerAccountId", customerAccountId);
            stub.setHeader(namespace, "ApplicationToken", applicationToken);

            // Set body parameters
            setNegativeSitesToCampaignsRequest = new SetNegativeSitesToCampaignsRequest();
            setNegativeSitesToCampaignsRequest.setAccountId(customerAccountId);
            // Parse campaignNegativeSites from XML
            campaignNegativeSites = util.campaignNegativeSitesFromXML(campaignNegativeSitesXML); // IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException
            setNegativeSitesToCampaignsRequest.setCampaignNegativeSites(campaignNegativeSites);

            // Call WS
            campaignManagement.setNegativeSitesToCampaigns(setNegativeSitesToCampaignsRequest); // RemoteException, ApiFaultDetail, AdApiFaultDetail

            // Call succeeded with no errors

            // Create the return object

            for (int i=0; i<campaignNegativeSites.length; i++) {
                GA_ResultItem resultItem =  new GA_ResultItem();
                resultItem.SubCampaignGxId = ((GxCampaignNegativeSites) campaignNegativeSites[i]).getGxCampaignId();
                resultItem.CampaignId = campaignNegativeSites[i].getCampaignId();

                result.Items.add(resultItem);
            }
        } catch (GxException e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        } catch (ApiFaultDetail fault) { // IN CALL
            for (int i=0; i<setNegativeSitesToCampaignsRequest.getCampaignNegativeSites().length; i++) {
                GA_ResultItem resultItem = new GA_ResultItem(fault, i);

                resultItem.SubCampaignGxId =  ((GxCampaignNegativeSites) setNegativeSitesToCampaignsRequest.getCampaignNegativeSites()[i]).getGxCampaignId();

                result.Items.add(resultItem);
            }
        } catch (AdApiFaultDetail fault) { // IN CALL
            for (int i=0; i<setNegativeSitesToCampaignsRequest.getCampaignNegativeSites().length; i++) {
                GA_ResultItem resultItem = new GA_ResultItem(fault);

                resultItem.SubCampaignGxId = ((GxCampaignNegativeSites) setNegativeSitesToCampaignsRequest.getCampaignNegativeSites()[i]).getGxCampaignId();

                result.Items.add(resultItem);
            }
        } catch (RemoteException e) { // IN CALL
            for (int i=0; i<setNegativeSitesToCampaignsRequest.getCampaignNegativeSites().length; i++) {
                GA_ResultItem resultItem = new GA_ResultItem(e);

                resultItem.SubCampaignGxId =  ((GxCampaignNegativeSites) setNegativeSitesToCampaignsRequest.getCampaignNegativeSites()[i]).getGxCampaignId();

                result.Items.add(resultItem);
            }
        } catch (Exception e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        }

        String returnMessage = null;
        // GxIdResults -> XML
        try {
            JAXBContext contextObj = JAXBContext.newInstance(GA_Result.class); // JAXBException

            Marshaller marshallerObj = contextObj.createMarshaller(); // JAXBException
            marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // PropertyException

            StringWriter sw = new StringWriter();

            marshallerObj.marshal(result, sw); // JAXBException
            returnMessage = sw.getBuffer().toString();
        }  catch (PropertyException e) { // AFTER CALL
            returnMessage = util.getPropertyExceptionMessage(e);
        } catch (JAXBException e) { // AFTER CALL
            returnMessage = util.getJAXBExceptionMessage(e);
        }
        return returnMessage;
    }

    public String deleteCampaigns(long customerAccountId, String campaignsXML) {
        ICampaignManagementService campaignManagement = null;
        CampaignManagementServiceLocator campaignServiceLocator = null;
        BasicHttpBinding_ICampaignManagementServiceStub stub = null;
        DeleteCampaignsRequest deleteCampaignsRequest = null;
        GA_Result result = new GA_Result();
        Util util = new Util();
        Campaign[] campaigns = null;
        try {
            HashMap<String, String> properties = Info.getBingProperties();
            String namespace = properties.get("campaignNamespace");
            String url = properties.get("campaignURL");
            String userName = properties.get("username");
            String password = properties.get("password");
            String developerToken = properties.get("developerToken");
            String customerId = properties.get("customerId");
            String applicationToken = properties.get("applicationToken");

            campaignServiceLocator = new CampaignManagementServiceLocator();
            campaignServiceLocator.setBasicHttpBinding_ICampaignManagementServiceEndpointAddress(url);
            campaignManagement = campaignServiceLocator.getBasicHttpBinding_ICampaignManagementService(); // ServiceException
            stub = (BasicHttpBinding_ICampaignManagementServiceStub) campaignManagement;

            // Set header parameters
            stub.setHeader(namespace, "UserName", userName);
            stub.setHeader(namespace, "Password", password);
            stub.setHeader(namespace, "DeveloperToken", developerToken);
            stub.setHeader(namespace, "CustomerId", customerId);
            stub.setHeader(namespace, "CustomerAccountId", customerAccountId);
            stub.setHeader(namespace, "ApplicationToken", applicationToken);

            // Set body parameters
            deleteCampaignsRequest = new DeleteCampaignsRequest();
            deleteCampaignsRequest.setAccountId(customerAccountId);
            // Parse campaigns from XML
            campaigns = util.campaignsFromXML(campaignsXML); // IllegalArgumentException, GxException, ParserConfigurationException, SAXException, IOException
            long[] campaignIds = new long[campaigns.length];
            for (int i=0; i<campaignIds.length; i++) {
                if (campaigns[i].getId() == null) {
                    campaignIds[i] = -1;
                } else {
                    campaignIds[i] = campaigns[i].getId();
                }
            }
            deleteCampaignsRequest.setCampaignIds(campaignIds);

            // Call WS
            campaignManagement.deleteCampaigns(deleteCampaignsRequest); // RemoteException, ApiFaultDetail, AdApiFaultDetail

            // Call succeeded with no errors

            // Create the return object

            for(int i=0; i<campaigns.length; i++){
                GA_ResultItem resultItem = new GA_ResultItem();
                resultItem.SubCampaignGxId = ((GxCampaign)campaigns[i]).getGxId();
                resultItem.CampaignId = (campaigns[i]).getId();

                result.Items.add(resultItem);
            }
        } catch (GxException e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        } catch (ApiFaultDetail fault) { // IN CALL
            for (int i=0; i<campaigns.length; i++) {
                GA_ResultItem resultItem = new GA_ResultItem(fault, i);

                resultItem.SubCampaignGxId = ((GxCampaign)campaigns[i]).getGxId();

                result.Items.add(resultItem);
            }
        } catch (AdApiFaultDetail fault) { // IN CALL
            for (int i=0; i<campaigns.length; i++) {
                GA_ResultItem resultItem = new GA_ResultItem(fault);

                resultItem.SubCampaignGxId = ((GxCampaign)campaigns[i]).getGxId();

                result.Items.add(resultItem);
            }
        } catch (RemoteException e) { // IN CALL
            for (int i=0; i<deleteCampaignsRequest.getCampaignIds().length; i++) {
                GA_ResultItem resultItem = new GA_ResultItem(e);

                resultItem.CampaignId =  deleteCampaignsRequest.getCampaignIds()[i];

                result.Items.add(resultItem);
            }
        } catch (Exception e) { // BEFORE CALL
            result.Items.add(new GA_ResultItem(e));
        }

        String returnMessage = null;
        // GxIdResults -> XML
        try {
            JAXBContext contextObj = JAXBContext.newInstance(GA_Result.class); // JAXBException

            Marshaller marshallerObj = contextObj.createMarshaller(); // JAXBException
            marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // PropertyException

            StringWriter sw = new StringWriter();

            marshallerObj.marshal(result, sw); // JAXBException
            returnMessage = sw.getBuffer().toString();
        }  catch (PropertyException e) { // AFTER CALL
            returnMessage = util.getPropertyExceptionMessage(e);
        } catch (JAXBException e) { // AFTER CALL
            returnMessage = util.getJAXBExceptionMessage(e);
        }
        return returnMessage;
    }
}
